//
//  TXFPictureView.swift
//  weibo飞飞
//
//  Created by 田侠飞 on 16/6/30.
//  Copyright © 2016年 田侠飞. All rights reserved.
//

import UIKit

//
let  cellID = "cellID"
let itemMargin : CGFloat = 5
//全局的cell的行高:

let cellWH = (SCREENW -  StautsMargin * 2 - itemMargin * 2)/3
class TXFPictureView: UICollectionView {

    var pictureInfo : [TXFPictureInfo]?{
    
        didSet{
        
        //设置:
        
//            contentLab.text = "\(pictureInfo?.count ?? 0)"
            let size = getPictureSize(pictureInfo?.count ?? 0)
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(size)

                
            }
            reloadData()
            
        }
  
    }
    //设置frame:picture;
    func getPictureSize(count: Int)-> CGSize{
    
        let col = count == 4 ? 2 : count >= 3 ? 3 : count
        let row =  count == 4 ? 2 :(count - 1)/3 + 1
        
        let w = CGFloat(col) * cellWH + CGFloat(col - 1) * itemMargin
        let h = CGFloat(row) * cellWH + CGFloat(row - 1) * itemMargin
        
        return CGSizeMake(w, h)
        
    }
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
      super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //创建一个label来进行测试图片的个数:
    
    private lazy var contentLab : UILabel = UILabel(textColor: UIColor.redColor(), textFont: 35)
    func setupUI(){
    
    
    self.backgroundColor = UIColor.clearColor()
    //设置每个Item的属性
        
   
        
          //数据源方法
        
        dataSource = self
        
         //注册一个类
        
        registerClass(TXFPictureViewCell.self, forCellWithReuseIdentifier: cellID)
       let flowLayout = collectionViewLayout as! UICollectionViewFlowLayout
        flowLayout.itemSize = CGSize(width: cellWH, height: cellWH)
        
        flowLayout.minimumInteritemSpacing = itemMargin
        
        flowLayout.minimumLineSpacing = itemMargin
        addSubview(contentLab)
        contentLab.snp_makeConstraints { (make) -> Void in
            
            
            make.center.equalTo(self)
        }
        
      
        
        
               
    
    }
    

}

extension TXFPictureView: UICollectionViewDataSource{

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pictureInfo?.count ?? 0
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellID, forIndexPath: indexPath) as! TXFPictureViewCell
        cell.picture  =  pictureInfo![indexPath.item]
        
      return cell
        
    }
}




class TXFPictureViewCell: UICollectionViewCell {
    
    var picture: TXFPictureInfo?{
    
    
        didSet{
        
        
        
        bgImageView.sd_setImageWithURL(NSURL(string: picture?.thumbnail_pic ?? ""), placeholderImage: UIImage(named: "timeline_card_top_background"))
        
        }
    
    
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private lazy var bgImageView : UIImageView = {
    let img = UIImageView(image: UIImage(named: "timeline_card_top_background"))
    img.contentMode = .ScaleAspectFill
    img.clipsToBounds = true
    return img
    }()
    
    //创建UI控件
    
    func setupUI(){
    
    
    contentView.addSubview(bgImageView)
        
        bgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
    
    
    
    
    }
    
    
    
    
    
    
    
    
    
}